// Copyright (C) 2010  Joan Puigcerver Perez, <joapuipe@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#ifndef __TCPSERVER_HPP__
#define __TCPSERVER_HPP__

#include <iostream>
#include <cc++/socket.h>

#include <LogUtils.hpp>

/**
 * \class TCPServer
 *
 * This class is derivated from TCPSocket and it is used to 
 * accept connections from a choosen port.
 *
 * Using the method isPendingConnection the object waits for
 * a connection. When a connection is accepted then, the method
 * onAccept is executed.
 *
 * Is the duty of the programmer to manage that connection using
 * a TCPClientSession object.
 *
 * \see TCPClientSession 
 */
class TCPServer: public ost::TCPSocket {
protected:
  /**
   * This function is executed when a connection is accepted.
   * It simply shows the address of the connected client and the port used
   * to listen it.
   */
  bool onAccept(const ost::InetHostAddress &ia, ost::tpport_t port);
private:
  std::ostream& logfile;
  ost::Mutex& log_mutex;
public:
  /**
   * Constructor. 
   * @param addr The server listens on this address.
   * @param port The server listens on this port.
   * @param logf Output stream used to print log messages.
   * @param logm Mutex used to assure that no other line is printed at the same moment on logf.
   */
  TCPServer(ost::InetAddress &addr, ost::tpport_t port = 4242, 
	    std::ostream& logf = std::cout, ost::Mutex& logm = LOGmutex);
};



#endif
